package com.xyht.sca_s.student_manage_system.modules.mailbox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.mailbox.entity.SmsMailbox;
import com.xyht.sca_s.student_manage_system.modules.mailbox.entity.dto.SmsReplyMailboxDto;
import com.xyht.sca_s.student_manage_system.modules.mailbox.entity.dto.SmsSendMailboxDto;
import com.xyht.sca_s.student_manage_system.modules.mailbox.entity.vo.SmsMailboxDetailVo;
import com.xyht.sca_s.student_manage_system.modules.mailbox.entity.vo.SmsReplyAllMailboxVo;
import com.xyht.sca_s.student_manage_system.modules.mailbox.entity.vo.SmsSendAllMailboxVo;
import com.xyht.sca_s.student_manage_system.modules.mailbox.mapper.SmsMailboxMapper;
import com.xyht.sca_s.student_manage_system.modules.mailbox.service.SmsMailboxService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2022-12-26
 */
@Service
public class SmsMailboxServiceImpl extends ServiceImpl<SmsMailboxMapper, SmsMailbox> implements SmsMailboxService {

    @Resource
    private SmsMailboxMapper smsMailboxMapper;

    @Override
    public ResponseResult addMailbox(SmsSendMailboxDto smsSendMailboxDto, String sender) {
        //校验参数是否为空
        if (isNullOrEmpty(smsSendMailboxDto.getTitle()) || isNullOrEmpty(smsSendMailboxDto.getSendContent())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsMailbox smsMailbox = new SmsMailbox();
        BeanUtils.copyProperties(smsSendMailboxDto, smsMailbox);
        smsMailbox.setSendTime(new Date());
        smsMailbox.setCreateTime(new Date());
        String senderName = smsMailboxMapper.selectSenderById(sender);
        smsMailbox.setSender(senderName);
        smsMailbox.setSendUserId(sender);
        smsMailboxMapper.insert(smsMailbox);
        return CommonResult.success();
    }

    @Override
    public ResponseResult selectMyAllMailbox(String sender, Integer pageSize, Integer pageNum) {
        Page<SmsMailbox> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsMailbox> queryWrapper = new QueryWrapper<>();
        //查数据库是否有数据
//        List<SmsMailbox> smsMailboxes = smsMailboxMapper.selectList(queryWrapper);
//        if(smsMailboxes == null){
//            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
//        }
        //查询条件
//        String senderName = smsMailboxMapper.selectSenderById(sender);
        queryWrapper
                .lambda()
                .eq(SmsMailbox::getSendUserId, sender)
                .orderByDesc(SmsMailbox::getSendTime);
        smsMailboxMapper.selectPage(page, queryWrapper);
        List<SmsSendAllMailboxVo> smsSendAllMailboxVos = new ArrayList<>();
        List<SmsMailbox> list = page.getRecords();
        for (SmsMailbox smsMailbox : list) {
            SmsSendAllMailboxVo smsSendAllMailboxVo = new SmsSendAllMailboxVo();
            BeanUtils.copyProperties(smsMailbox, smsSendAllMailboxVo);
            if (smsMailbox.getAnonymous() == 0) {
                smsSendAllMailboxVo.setSender("匿名用户");
            }
            smsSendAllMailboxVos.add(smsSendAllMailboxVo);
        }
        int total = (int) page.getTotal();
        return CommonResult.success(smsSendAllMailboxVos,total);
    }

    @Override
    public ResponseResult selectMailboxById(String id) {
        if (isNullOrEmpty(id)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsMailbox smsMailbox = smsMailboxMapper.selectById(id);
        if (smsMailbox == null) {
            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
        }
        smsMailboxMapper.updateView(smsMailbox.getView(), id);
        SmsMailboxDetailVo smsMailboxDetailVo = new SmsMailboxDetailVo();
        BeanUtils.copyProperties(smsMailbox, smsMailboxDetailVo);
        if (smsMailbox.getAnonymous() == 0) {
            smsMailboxDetailVo.setSender("匿名用户");
        }
        return CommonResult.success(smsMailboxDetailVo);
    }

    @Override
    public ResponseResult selectAllOpenMailbox(String sender, Integer pageSize, Integer pageNum) {
        Page<SmsMailbox> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsMailbox> queryWrapper = new QueryWrapper<>();
        //查数据库是否有数据
//        List<SmsMailbox> smsMailboxes = smsMailboxMapper.selectList(queryWrapper);
//        if (smsMailboxes == null) {
//            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
//        }
        //查询条件
        String senderName = smsMailboxMapper.selectSenderById(sender);
        queryWrapper.lambda().eq(SmsMailbox::getIsOpen, 1)
                .ne(SmsMailbox::getSender, senderName)
                .orderByDesc(SmsMailbox::getSendTime);
        smsMailboxMapper.selectPage(page, queryWrapper);
        List<SmsSendAllMailboxVo> smsSendAllMailboxVos = new ArrayList<>();
        List<SmsMailbox> list = page.getRecords();
        for (SmsMailbox smsMailbox : list) {
            SmsSendAllMailboxVo smsSendAllMailboxVo = new SmsSendAllMailboxVo();
            BeanUtils.copyProperties(smsMailbox, smsSendAllMailboxVo);
            if (smsMailbox.getAnonymous() == 0) {
                smsSendAllMailboxVo.setSender("匿名用户");
            }
            smsSendAllMailboxVos.add(smsSendAllMailboxVo);
        }
        int total = (int) page.getTotal();
        return CommonResult.success(smsSendAllMailboxVos,total);
    }

    @Override
    public ResponseResult selectAllMailbox(Integer pageSize, Integer pageNum) {
        Page<SmsMailbox> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsMailbox> queryWrapper = new QueryWrapper<>();
        //查数据库是否有数据
        List<SmsMailbox> smsMailboxes = smsMailboxMapper.selectList(queryWrapper);
        if (smsMailboxes == null) {
            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
        }
        queryWrapper.lambda().orderByDesc(SmsMailbox::getSendTime);
        smsMailboxMapper.selectPage(page, queryWrapper);
        List<SmsReplyAllMailboxVo> SmsReplyAllMailboxVos = new ArrayList<>();
        List<SmsMailbox> list = page.getRecords();
        for (SmsMailbox smsMailbox : list) {
            SmsReplyAllMailboxVo smsReplyAllMailboxVo = new SmsReplyAllMailboxVo();
            BeanUtils.copyProperties(smsMailbox, smsReplyAllMailboxVo);
            if (smsMailbox.getAnonymous() == 0) {
                smsReplyAllMailboxVo.setSender("匿名用户");
            }
            SmsReplyAllMailboxVos.add(smsReplyAllMailboxVo);
        }
        int total = (int) page.getTotal();
        return CommonResult.success(SmsReplyAllMailboxVos,total);
    }

    @Override
    public ResponseResult deleteMailboxById(SmsMailbox smsMailbox) {
        if (isNullOrEmpty(smsMailbox.getId())) {
            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
        }
        int result = smsMailboxMapper.deleteById(smsMailbox.getId());
        if (result == 0) {
            return CommonResult.failed(CommonCodeEnum.DELETE_FAIL);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult addReplyMailbox(SmsReplyMailboxDto smsReplyMailboxDto) {
        if (isNullOrEmpty(smsReplyMailboxDto.getReplyContent())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if (isNullOrEmpty(smsReplyMailboxDto.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //查询数据库中的通知
        SmsMailbox mailbox = smsMailboxMapper.selectById(smsReplyMailboxDto.getId());
        if (mailbox == null) {
            log.debug("信箱不存在,回复失败");
            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
        }

        SmsMailbox smsMailbox = new SmsMailbox();
        BeanUtils.copyProperties(smsReplyMailboxDto, smsMailbox);
        //已回复状态
        smsMailbox.setTitle(mailbox.getTitle());
        smsMailbox.setAnonymous(mailbox.getAnonymous());
        smsMailbox.setSendContent(mailbox.getSendContent());
        if (smsMailbox.getAnonymous() == 0) {
            smsMailbox.setSender("匿名用户");
        } else {
            smsMailbox.setSender(mailbox.getSender());
        }
        smsMailbox.setStatus("1");
        smsMailbox.setSendTime(mailbox.getSendTime());
        smsMailbox.setReplyTime(new Date());
        smsMailbox.setView(mailbox.getView());
        int result = smsMailboxMapper.updateById(smsMailbox);
        if (result == 0) {
            return CommonResult.failed(CommonCodeEnum.UPDATE_FAIL);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult selectOpenMailbox(Integer pageSize, Integer pageNum, String title) {
        Page<SmsMailbox> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsMailbox> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.lambda().eq(SmsMailbox::getIsOpen, 1).orderByDesc(SmsMailbox::getSendTime);
        smsMailboxMapper.selectPage(page, queryWrapper);
        List<SmsMailbox> mailboxs = page.getRecords();
        if (mailboxs == null) {
            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
        }
        List<SmsSendAllMailboxVo> smsSendAllMailboxVos = new ArrayList<>();
        for (SmsMailbox mailbox : mailboxs) {
            if (mailbox.getIsOpen() == 1) {
                SmsSendAllMailboxVo smsSendAllMailboxVo = new SmsSendAllMailboxVo();
                BeanUtils.copyProperties(mailbox, smsSendAllMailboxVo);
                if (mailbox.getAnonymous() == 0) {
                    smsSendAllMailboxVo.setSender("匿名用户");
                }
                smsSendAllMailboxVos.add(smsSendAllMailboxVo);
            }
        }
        int total = (int) page.getTotal();
        return CommonResult.success(smsSendAllMailboxVos,total);
    }

    @Override
    public ResponseResult selectMyMailbox(Integer pageSize, Integer pageNum, String title, String sender) {
        Page<SmsMailbox> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsMailbox> queryWrapper = new QueryWrapper<>();
        ;
        String senderName = smsMailboxMapper.selectSenderById(sender);
        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.lambda().eq(SmsMailbox::getSender, senderName)
                .orderByDesc(SmsMailbox::getSendTime);
        smsMailboxMapper.selectPage(page, queryWrapper);
        List<SmsMailbox> mailboxs = page.getRecords();
        if (mailboxs == null) {
            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
        }
        List<SmsSendAllMailboxVo> smsSendAllMailboxVos = new ArrayList<>();
        for (SmsMailbox mailbox : mailboxs) {
            if (mailbox.getSender().equals(senderName)) {
                SmsSendAllMailboxVo smsSendAllMailboxVo = new SmsSendAllMailboxVo();
                BeanUtils.copyProperties(mailbox, smsSendAllMailboxVo);
                if (mailbox.getAnonymous() == 0) {
                    smsSendAllMailboxVo.setSender("匿名用户");
                }
                smsSendAllMailboxVos.add(smsSendAllMailboxVo);
            }
        }
        int total = (int) page.getTotal();
        return CommonResult.success(smsSendAllMailboxVos,total);
    }

    @Override
    public ResponseResult switchIsOpen(SmsMailbox smsMailbox,String user_id) {
        if (isNullOrEmpty(smsMailbox.getId()) || isNullOrEmpty(smsMailbox.getIsOpen())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsMailbox smsMailbox1 = smsMailboxMapper.selectById(smsMailbox.getId());
        if (smsMailbox1 == null || smsMailbox1.getId() == null){
            return CommonResult.failed(CommonCodeEnum.MAILBOX_NOT_EXIST);
        }

        SmsMailbox newSmsMailBox = new SmsMailbox();
        newSmsMailBox.setId(smsMailbox.getId());
        newSmsMailBox.setIsOpen(smsMailbox.getIsOpen());
        smsMailboxMapper.updateById(newSmsMailBox);
        return CommonResult.success();
    }


}
